export function exportExcel(columns,list,filename){
    require.ensure([], () => {
        const { export_json_to_excel } = require('../../utils/excel/export2Excel');
        let tHeader = []
        let filterVal = []
        console.log(columns)
        if(!columns){
            return;
        }
        columns.forEach(item =>{
            tHeader.push(item.caption)
            filterVal.push(item.propertyName||item.fieldName)
        })
        const data = list.map(v => filterVal.map(j => v[j]))
        export_json_to_excel(tHeader, data, filename);
    })
}

export function exportTreeExcel(columns,list,filename,childrenName){
    require.ensure([], () => {
        const { export_json_to_excel } = require('../../utils/excel/export2Excel');
        let tHeader = []
        let filterVal = []
        console.log(columns)
        if(!columns){
            return;
        }
        columns.forEach(item =>{
            tHeader.push(item.label)
            if(columns.formatter){
                filterVal.push(columns.formatter(item.propertyName || item.prop));
            }else {
                filterVal.push(item.propertyName || item.prop)
            }
        })
        let newData = convertTreeToTable(list);
        const data = newData.map(v => filterVal.map(j => v[j]))
        export_json_to_excel(tHeader, data, filename);
    })
}

function convertTreeToTable(treeTable,childrenName){
    if(!childrenName){
        childrenName = 'children'
    }
    let table = [];
    for(let row of treeTable){
        table.push(row);
        if(row[childrenName]){
            let children = convertTreeToTable(row[childrenName]);
            if(children && children.length > 0){
                table = table.concat(children);
            }
        }
    }
    return table;
}